package com.giibpt;

import java.util.ArrayList;
import java.util.Date;

import com.giibpt.model.HibernateUtil;
import com.giibpt.model.entities.Area;
import com.giibpt.model.entities.Evento;
import com.giibpt.model.entities.SubArea;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.DateField;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.Window.Notification;

public class ConsultaXArea extends Panel{

	private Button btnSearch;
	private DateField txtDate;
	private ComboBox cmbArea;
	private ComboBox cmbSubArea;
	private Table tblResult;
	private BeanItemContainer<SubArea> containerSubareas;
	public ConsultaXArea() {
	
		txtDate = new DateField();
		txtDate.setResolution(DateField.RESOLUTION_DAY);
	
		BeanItemContainer<Area> container = new BeanItemContainer<Area>(Area.class);
		container.addAll(new ArrayList(HibernateUtil.list(Area.class)));
		
		cmbArea = new ComboBox("Area");
		cmbArea.setContainerDataSource(container);
		cmbArea.setItemCaptionPropertyId("nombre");
		cmbArea.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_PROPERTY);
		cmbArea.setImmediate(true);
		cmbArea.setRequired(true);
		cmbArea.addListener(new Property.ValueChangeListener() {		
			public void valueChange(ValueChangeEvent event) {
				refreshSubAreas();
			}
		});
		containerSubareas = new BeanItemContainer<SubArea>(SubArea.class);
		cmbSubArea = new ComboBox("SubArea");
		cmbSubArea.setContainerDataSource(containerSubareas);
		cmbSubArea.setItemCaptionPropertyId("nombre");
		cmbSubArea.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_PROPERTY);
		
		btnSearch = new Button("Buscar");
		btnSearch.addListener(new Button.ClickListener() {
			
			public void buttonClick(ClickEvent event) {
				search();
			}
		});
		
		tblResult = new Table();
		tblResult.setContainerDataSource(new BeanItemContainer<Evento>(Evento.class));
		tblResult.addGeneratedColumn("visitante", new Table.ColumnGenerator() {
			public Component generateCell(Table source, Object itemId, Object columnId) {
				Evento evento = (Evento) itemId;
				Button button =  new Button(evento.getEtiqueta().toString());
				button.addStyleName("link");
				return button;
			}
		});
		
		tblResult.addGeneratedColumn("subarea", new Table.ColumnGenerator() {
			
			public Component generateCell(Table source, Object itemId, Object columnId) {
				Evento evento = (Evento) itemId;
				return new Label(evento.getLector().getSubArea().getNombre());
			}
		});
		
		tblResult.addGeneratedColumn("lector", new Table.ColumnGenerator() {
			
			public Component generateCell(Table source, Object itemId, Object columnId) {
				Evento evento = (Evento) itemId;
				return new Label(evento.getLector().getIdLector());
			}
		});
		
		tblResult.addGeneratedColumn("tipoEvento.nombre", new Table.ColumnGenerator() {
			
			public Component generateCell(Table source, Object itemId, Object columnId) {
				Evento evento = (Evento) itemId;
				return new Label(evento.getTipoEvento().getNombre());
			}
		});
		
		tblResult.setVisibleColumns(new Object[]{"fecha", "visitante", "subarea", "tipoEvento.nombre",  "lector"});
		
		GridLayout lytMain = new GridLayout(1, 5);
	
		lytMain.addComponent(cmbArea, 0,0);
		lytMain.addComponent(cmbSubArea, 0,1);
		lytMain.addComponent(txtDate, 0,2);
		lytMain.addComponent(btnSearch, 0, 3);
		lytMain.addComponent(tblResult, 0, 4);
		
		setContent(lytMain);
		
		
	}
	private void refreshSubAreas(){
		Area area = (Area)cmbArea.getValue();
		if(area != null){
			cmbSubArea.setValue(null);
			containerSubareas.removeAllItems();
			containerSubareas.addAll(new ArrayList<SubArea>(HibernateUtil.getSubAreas(area))); 
		}
	}
	
	private void search(){
		
		if(!cmbArea.isValid()){
			getWindow().showNotification("El Area es obligatoria", Notification.TYPE_WARNING_MESSAGE);
			return;
		}
		BeanItemContainer<Evento> container = (BeanItemContainer<Evento>) tblResult.getContainerDataSource();
		container.removeAllItems();
		
		container.addAll(new ArrayList(HibernateUtil.getEventos(null, (Date) txtDate.getValue(), (Area)cmbArea.getValue(), (SubArea)cmbSubArea.getValue())));
	}
	
	

}
